import { defineStore } from 'pinia';

export const useUserStore = defineStore('user', {
  state: () => ({
    userInfo: null,
    token: null,
    isLoggedIn: false
  }),
  getters: {
    // 将userRole改为getter，确保正确获取用户角色
    userRole: (state) => state.userInfo?.userRole || ''
  },
  actions: {
    setUserInfo(info) {
      this.userInfo = info;
      this.isLoggedIn = true;
      localStorage.setItem('userInfo', JSON.stringify(info));
    },
    setToken(token) {
      this.token = token;
      localStorage.setItem('token', token);
    },
    logout() {
      this.userInfo = null;
      this.token = null;
      this.isLoggedIn = false;
      localStorage.removeItem('userInfo');
      localStorage.removeItem('token');
    }
  }
});